home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / tests / tplex.cc < prev    next >
C/C++ Source or Header  |  1991-06-28  |  13KB  |  715 lines

  1. /*
  2.   test of Plexes
  3. */
  4.  
  5. #include <stream.h>
  6. #include <assert.h>
  7. #include "iPlex.h"
  8.  
  9. #define tassert(ex) {if ((ex)) cerr << #ex << "\n"; \
  10.                        else _assert(#ex, __FILE__,__LINE__); }
  11.  
  12.  
  13. void printplex(intPlex& a)
  14. {
  15.   cout << "[";
  16.   int maxprint = 20;
  17.   int k = 0;
  18.   for (int i = a.low(); i < a.fence() && k < maxprint; ++i, ++k)
  19.     cout << a[i] << " ";
  20.   if (i < a.fence()) cout << "]\n";
  21.   else cout << "...]\n";
  22. }
  23.  
  24. #include "iFPlex.h"
  25.  
  26. void FPtest () 
  27. {
  28.   intFPlex p(50);
  29.   assert(p.OK());
  30.   assert (p.empty());
  31.  
  32.   p.add_high (1000);
  33.   Pix px = p.index_to_Pix(0);
  34.   assert (p.length() == 1);
  35.   assert(p.owns(px));
  36.   assert(p(px) == 1000);
  37.   assert(p.Pix_to_index(px) == 0);
  38.  
  39.   p.reset_low (10);
  40.   assert (p.length() == 1);
  41.   assert (p.low() == 10);
  42.   assert (p[10] == 1000);
  43.   assert(p(px) == 1000);
  44.   assert(p.Pix_to_index(px) == 10);
  45.   assert(p.index_to_Pix(10) == px);
  46.   assert(p.OK());
  47.  
  48.   int h = p.high_element();
  49.   int l = p.low_element();
  50.   assert ( (h==l) && (h==1000));
  51.   p.fill(222);
  52.   assert(p(px) == 222);
  53.  
  54.   p.del_high();   
  55.   assert(p.empty());
  56.   assert(!p.owns(px));
  57.  
  58.   intFPlex q(10, -50);
  59.   q.add_low (-1000);
  60.   assert (q[9] == -1000);
  61.   q[9] = 21;
  62.   assert(!q.valid(10));
  63.   assert(q.OK());
  64.  
  65.   q.del_low();
  66.   assert (q.empty());
  67.  
  68.   p.reset_low (0);
  69.   q.reset_low (0);
  70.  
  71.   for (int i = 0; i < 50; i++) 
  72.   {
  73.     if (i % 2 == 0) 
  74.     { 
  75.       p.add_high (i);
  76.       assert (p.high() == i/2);
  77.       p[i/2] = p[i/4];
  78.       assert (p.high_element() == i/4);
  79.       p[i/2] = i/2;
  80.     } 
  81.     else 
  82.     {
  83.       q.add_low (-i);
  84.       int ii = - (i/2) -1;
  85.       assert (q.low() == ii);
  86.       q.low_element() = ii;
  87.       assert (q[ii] == ii);
  88.     }
  89.   }
  90.  
  91.   cout << "q:"; printplex(q);
  92.   assert (p.length() == 25);
  93.   assert (q.length() == 25);
  94.  
  95.   assert(p.valid(0));
  96.   assert(p.owns(px));
  97.   
  98.   px = p.first();
  99.   i = 0;
  100.   for (int it1 = p.low(); it1 < p.fence(); p.next(it1))
  101.   {
  102.     assert (p[it1] == it1);
  103.     assert(p(px) == p[it1]);
  104.     p.next(px);
  105.     ++i;
  106.   }
  107.   assert(px == 0);
  108.   px = q.last();
  109.   for (it1 = q.high(); it1 > q.ecnef(); q.prev(it1))
  110.   {
  111.     assert (q[it1] == it1);
  112.     assert(p(px) == q[it1]);
  113.     q.prev(px);
  114.     ++i;
  115.   }
  116.   assert(i == 50);
  117.   q.reset_low (0);  
  118.   assert (p.high() == q.high());
  119.   assert(p.low() == 0);
  120.   
  121.   intFPlex p1 = p;
  122.   intFPlex p2 (p);
  123.   intFPlex p3 = p2;
  124.   assert (p1.length() == 25);
  125.   assert (p1.high() == 24);
  126.   assert(p1.low() == 0);
  127.   assert(p.OK());
  128.   assert(p1.OK());
  129.   assert(p2.OK());
  130.   assert(p3.OK());
  131.  
  132.   i = 0;
  133.   for (int it5 = p.low(); it5 < p.fence(); p.next(it5)) 
  134.   {
  135.     assert(p1.low() == it5);
  136.     p1.del_low();
  137.     assert(!p1.valid(it5));
  138.     p2.del_high ();
  139.     p3 [it5] = -it5;
  140.     ++i;
  141.   }
  142.   assert(i == 25);
  143.  
  144.   assert(p.OK());
  145.   assert(p1.OK());
  146.   assert(p2.OK());
  147.   assert(p3.OK());
  148.   assert (p1.empty());
  149.   assert (p2.empty());
  150.  
  151.   p3.append (p);
  152.   assert(p3.OK());
  153.  
  154.   p1.prepend (p);
  155.   p2.append (p);
  156.   assert(p1.length() == p.length());
  157.   assert(p1.length() == p2.length());
  158.   assert(p1.OK());
  159.   assert(p2.OK());
  160.  
  161.   p2.clear();
  162.   assert(p2.OK());
  163.   assert(p2.empty());
  164.   p2 = p1;
  165.   assert(p2.OK());
  166.  
  167.   p1 = p;
  168.   assert(p1.OK());
  169.   p1.reset_low (p1.low_element());
  170.   for (int it6 = p1.low(); it6 < p1.fence(); it6++)
  171.   {
  172.     assert (p1[it6] == it6);
  173.   }
  174.   p1[13] = 1313;
  175.   p1[7] = -7777;
  176.   p1[24] = 24242424;
  177.   assert(!p1.valid(25));
  178.   assert(!p1.valid(-1));
  179.   assert(p1.OK());
  180.  
  181. }
  182.  
  183. #include "iXPlex.h"
  184.  
  185. void XPtest () 
  186. {
  187.   intXPlex p(3);
  188.   assert(p.OK());
  189.   assert (p.empty());
  190.  
  191.   p.add_high (1000);
  192.   Pix px = p.index_to_Pix(0);
  193.   assert(p.Pix_to_index(px) == 0);
  194.   assert (p.length() == 1);
  195.   assert(p.owns(px));
  196.   assert(p(px) == 1000);
  197.  
  198.   p.reset_low(10);
  199.   assert (p.length() == 1);
  200.   assert (p.low() == 10);
  201.   assert (p[10] == 1000);
  202.   assert(p(px) == 1000);
  203.   assert(p.Pix_to_index(px) == 10);
  204.   assert(p.index_to_Pix(10) == px);
  205.   assert(p.OK());
  206.  
  207.   int h = p.high_element();
  208.   int l = p.low_element();
  209.   assert ( (h==l) && (h==1000));
  210.   p.fill(222);
  211.   assert(p(px) == 222);
  212.  
  213.   p.del_high();   
  214.   assert(p.empty());
  215.   assert(!p.owns(px));
  216.  
  217.   p.add_low(-1000);
  218.   assert (p[9] == -1000);
  219.   p[9] = 21;
  220.   assert(!p.valid(10));
  221.   assert(p.OK());
  222.  
  223.   p.del_low();
  224.   assert (p.empty());
  225.   p.reset_low (0);
  226.  
  227.   for (int i = 0; i < 50; i++) 
  228.   {
  229.     if (i % 2 == 0) 
  230.     { 
  231.       p.add_high (i);
  232.       assert (p.high() == i/2);
  233.       p[i/2] = p[i/4];
  234.       assert (p.high_element() == i/4);
  235.       p[i/2] = i/2;
  236.     } 
  237.     else 
  238.     {
  239.       p.add_low (-i);
  240.       int ii = - (i/2) -1;
  241.       assert (p.low() == ii);
  242.       p.low_element() = ii;
  243.       assert (p[ii] == ii);
  244.     }
  245.   }
  246.  
  247.   assert (p.length() == 50);
  248.   cout << "p:"; printplex(p);
  249.  
  250.   assert(p.valid(0));
  251.   assert(p.owns(px));
  252.  
  253.   px = p.first();
  254.   i = 0;
  255.   for (int it1 = p.low(); it1 < p.fence(); p.next(it1))
  256.   {
  257.     assert (p[it1] == it1);
  258.     assert(p(px) == p[it1]);
  259.     p.next(px);
  260.     ++i;
  261.   }
  262.   assert(i == 50);
  263.   assert(px == 0);
  264.   p.reset_low (0);  
  265.   assert (p.high() == 49);
  266.   assert(p.low() == 0);
  267.   
  268.   i = 0;
  269.   for (int it2 = p.high(); it2 > p.ecnef(); p.prev(it2))
  270.   {
  271.     assert ( p[it2] == it2-25 );
  272.     ++i;
  273.   }
  274.   assert(i == 50);
  275.   assert(p.OK());
  276.   
  277.   intXPlex p1 = p;
  278.   intXPlex p2 (p);
  279.   intXPlex p3 = p2;
  280.   assert (p1.length() == 50);
  281.   assert (p1.high() == 49);
  282.   assert(p1.low() == 0);
  283.   assert(p.OK());
  284.   assert(p1.OK());
  285.   assert(p2.OK());
  286.   assert(p3.OK());
  287.  
  288.   i = 0;
  289.   for (int it5 = p.low(); it5 < p.fence(); p.next(it5)) 
  290.   {
  291.     assert(p1.low() == it5);
  292.     p1.del_low();
  293.     assert(!p1.valid(it5));
  294.     p2.del_high ();
  295.     p3 [it5] = -it5;
  296.     ++i;
  297.   }
  298.   assert(i == 50);
  299.  
  300.   assert(p.OK());
  301.   assert(p1.OK());
  302.   assert(p2.OK());
  303.   assert(p3.OK());
  304.   assert (p1.empty());
  305.   assert (p2.empty());
  306.  
  307.   p3.append (p);
  308.   assert(p3.OK());
  309.  
  310.   p1.prepend (p);
  311.   p2.append (p);
  312.   assert(p1.length() == p.length());
  313.   assert(p1.length() == p2.length());
  314.   assert(p1.OK());
  315.   assert(p2.OK());
  316.  
  317.   p2.clear();
  318.   assert(p2.OK());
  319.   assert(p2.empty());
  320.   p2 = p1;
  321.   assert(p2.OK());
  322.  
  323.  
  324.   p1 = p;
  325.   assert(p1.OK());
  326.   p1.reset_low(p1.low_element());
  327.   p1 [13] = 1313;
  328.   p1 [-7] = -7777;
  329.   p1 [-25] = -252525;
  330.   p1 [24] = 24242424;
  331.   assert(!p1.valid(25));
  332.   assert(!p1.valid(-26));
  333.   assert(p1.OK());
  334.  
  335.   p1 = p;
  336.   p1.reset_low (p1.low_element());
  337.   for (int it6 = p1.low(); it6 < p1.fence(); it6++)
  338.   {
  339.     assert (p1[it6] == it6);
  340.   }
  341.   p1.reverse();
  342.   i = p1.high();
  343.   for (it6 = p1.low(); it6 < p1.fence(); it6++)
  344.   {
  345.     assert (p1[it6] == i);
  346.     --i;
  347.   }
  348.   assert(p1.OK());
  349.  
  350. }
  351.  
  352. #include "iMPlex.h"
  353.  
  354. void MPtest () 
  355. {
  356.   intMPlex p(3);
  357.   assert(p.OK());
  358.   assert (p.empty());
  359.  
  360.   p.add_high (1000);
  361.   Pix px = p.index_to_Pix(0);
  362.   assert (p.length() == 1);
  363.   assert(p.owns(px));
  364.   assert(p(px) == 1000);
  365.   assert(p.Pix_to_index(px) == 0);
  366.  
  367.   p.reset_low (10);
  368.   assert (p.length() == 1);
  369.   assert (p.low() == 10);
  370.   assert (p[10] == 1000);
  371.   assert(p(px) == 1000);
  372.   assert(p.Pix_to_index(px) == 10);
  373.   assert(p.index_to_Pix(10) == px);
  374.   assert(p.OK());
  375.  
  376.   int h = p.high_element();
  377.   int l = p.low_element();
  378.   assert ( (h==l) && (h==1000));
  379.   p.fill(222);
  380.   assert(p(px) == 222);
  381.  
  382.   p.del_high();   
  383.   assert(p.empty());
  384.   assert(!p.owns(px));
  385.  
  386.   p.add_low (-1000);
  387.   assert (p[9] == -1000);
  388.   p[9] = 21;
  389.   assert(!p.valid(10));
  390.   assert(p.OK());
  391.  
  392.   p.del_low();
  393.   assert (p.empty());
  394.   p.reset_low (0);
  395.  
  396.   for (int i = 0; i < 50; i++) 
  397.   {
  398.     if (i % 2 == 0) 
  399.     { 
  400.       p.add_high (i);
  401.       assert (p.high() == i/2);
  402.       p[i/2] = p[i/4];
  403.       assert (p.high_element() == i/4);
  404.       p[i/2] = i/2;
  405.     } 
  406.     else 
  407.     {
  408.       p.add_low (-i);
  409.       int ii = - (i/2) -1;
  410.       assert (p.low() == ii);
  411.       p.low_element() = ii;
  412.       assert (p[ii] == ii);
  413.     }
  414.   }
  415.  
  416.   cout << "p:"; printplex(p);
  417.   assert (p.length() == 50);
  418.   assert (p.count() == 50);
  419.  
  420.   assert(p.available() == 0);
  421.   assert(p.valid(0));
  422.   px = &p[0];
  423.   assert(p.owns(px));
  424.   p.del_index(0);
  425.   assert(p.count() == 49);
  426.   assert(p.available() == 1);
  427.   assert(p.unused_index() == 0);
  428.   assert(!p.valid(0));
  429.   assert(!p.owns(px));
  430.   p.undel_index(0);
  431.   p[0] = 0;
  432.   assert(p.count() == 50);
  433.   assert(p.available() == 0);
  434.   assert(p.valid(0));
  435.   assert(p.owns(px));
  436.   assert(p.OK());
  437.  
  438.   p.del_index(0);
  439.   
  440.   px = p.first();
  441.   i = 0;
  442.   for (int it1 = p.low(); it1 < p.fence(); p.next(it1))
  443.   {
  444.     assert (p[it1] == it1);
  445.     assert(p(px) == p[it1]);
  446.     p.next(px);
  447.     ++i;
  448.   }
  449.   assert(i == 49);
  450.   assert(px == 0);
  451.   p.reset_low (0);  
  452.   assert (p.high() == 49);
  453.   assert(p.low() == 0);
  454.   
  455.   i = 0;
  456.   for (int it2 = p.high(); it2 > p.ecnef();  p.prev(it2))
  457.   {
  458.     assert ( p[it2] == it2-25 );
  459.     assert(px != &p[it2]);
  460.     ++i;
  461.   }
  462.   assert(i == 49);
  463.   assert(p.OK());
  464.   
  465.   p.del_index(1);
  466.   p.del_index(2);
  467.   assert (p.OK());
  468.   assert (p.count() == 47);
  469.  
  470.   intMPlex p1 = p;
  471.   intMPlex p2 (p);
  472.   intMPlex p3 = p2;
  473.   assert (p1.length() == 50);
  474.   assert (p1.count() == 47);
  475.   assert (p1.high() == 49);
  476.   assert(p1.low() == 0);
  477.   assert(p.OK());
  478.   assert(p1.OK());
  479.   assert(p2.OK());
  480.   assert(p3.OK());
  481.  
  482.   i = 0;
  483.   for (int it5 = p.low(); it5 < p.fence(); p.next(it5)) 
  484.   {
  485.     assert(p1.low() == it5);
  486.     p1.del_low();
  487.     assert(!p1.valid(it5));
  488.     p2.del_high ();
  489.     p3 [it5] = -it5;
  490.     ++i;
  491.   }
  492.   assert(i == 47);
  493.  
  494.   assert(p.OK());
  495.   assert(p1.OK());
  496.   assert(p2.OK());
  497.   assert(p3.OK());
  498.   assert (p1.empty());
  499.   assert (p2.empty());
  500.  
  501.   p3.append (p);
  502.   assert(p3.OK());
  503.  
  504.   p1.prepend (p);
  505.   p2.append (p);
  506.   assert(p1.count() == p.count());
  507.   assert(p1.count() == p2.count());
  508.   assert(p1.OK());
  509.   assert(p2.OK());
  510.  
  511.   p2.clear();
  512.   assert(p2.OK());
  513.   assert(p2.empty());
  514.   p2 = p1;
  515.   assert(p2.OK());
  516.  
  517.   p1 = p;
  518.   p1.del_index(3);
  519.   p1.del_index(4);
  520.   p1.del_index(5);
  521.   p1.del_index(6);
  522.   p1.del_index(7);
  523.   p1.del_index(8);
  524.   p1.undel_index(6);
  525.   p1[6] = 6666;
  526.   p1[9] = 9999;
  527.   p1[0] = 0;
  528.   assert(p1[6] == 6666);
  529.   assert(!p1.valid(5));
  530.   assert(!p1.valid(7));
  531.   p1.del_low();
  532.   assert(p1.low() == 6);
  533.   assert(p1.OK());
  534.  
  535. }
  536.  
  537. #include "iRPlex.h"
  538.  
  539. void RPtest () 
  540. {
  541.   intRPlex p(3);
  542.   assert(p.OK());
  543.   assert (p.empty());
  544.  
  545.   p.add_high (1000);
  546.   Pix px = p.index_to_Pix(0);
  547.   assert(p.Pix_to_index(px) == 0);
  548.   assert (p.length() == 1);
  549.   assert(p.owns(px));
  550.   assert(p(px) == 1000);
  551.  
  552.   p.reset_low(10);
  553.   assert (p.length() == 1);
  554.   assert (p.low() == 10);
  555.   assert (p[10] == 1000);
  556.   assert(p(px) == 1000);
  557.   assert(p.Pix_to_index(px) == 10);
  558.   assert(p.index_to_Pix(10) == px);
  559.   assert(p.OK());
  560.  
  561.   int h = p.high_element();
  562.   int l = p.low_element();
  563.   assert ( (h==l) && (h==1000));
  564.   p.fill(222);
  565.   assert(p(px) == 222);
  566.  
  567.   p.del_high();   
  568.   assert(p.empty());
  569.   assert(!p.owns(px));
  570.  
  571.   p.add_low(-1000);
  572.   assert (p[9] == -1000);
  573.   p[9] = 21;
  574.   assert(!p.valid(10));
  575.   assert(p.OK());
  576.  
  577.   p.del_low();
  578.   assert (p.empty());
  579.   p.reset_low (0);
  580.  
  581.   for (int i = 0; i < 50; i++) 
  582.   {
  583.     if (i % 2 == 0) 
  584.     { 
  585.       p.add_high (i);
  586.       assert (p.high() == i/2);
  587.       p[i/2] = p[i/4];
  588.       assert (p.high_element() == i/4);
  589.       p[i/2] = i/2;
  590.     } 
  591.     else 
  592.     {
  593.       p.add_low (-i);
  594.       int ii = - (i/2) -1;
  595.       assert (p.low() == ii);
  596.       p.low_element() = ii;
  597.       assert (p[ii] == ii);
  598.     }
  599.   }
  600.  
  601.   assert (p.length() == 50);
  602.   cout << "p:"; printplex(p);
  603.  
  604.   assert(p.valid(0));
  605.   assert(p.owns(px));
  606.  
  607.   px = p.first();
  608.   i = 0;
  609.   for (int it1 = p.low(); it1 < p.fence(); p.next(it1))
  610.   {
  611.     assert (p[it1] == it1);
  612.     assert(p(px) == p[it1]);
  613.     p.next(px);
  614.     ++i;
  615.   }
  616.   assert(i == 50);
  617.   assert(px == 0);
  618.   p.reset_low (0);  
  619.   assert (p.high() == 49);
  620.   assert(p.low() == 0);
  621.   
  622.   i = 0;
  623.   for (int it2 = p.high(); it2 > p.ecnef(); p.prev(it2))
  624.   {
  625.     assert ( p[it2] == it2-25 );
  626.     ++i;
  627.   }
  628.   assert(i == 50);
  629.   assert(p.OK());
  630.   
  631.   intRPlex p1 = p;
  632.   intRPlex p2 (p);
  633.   intRPlex p3 = p2;
  634.   assert (p1.length() == 50);
  635.   assert (p1.high() == 49);
  636.   assert(p1.low() == 0);
  637.   assert(p.OK());
  638.   assert(p1.OK());
  639.   assert(p2.OK());
  640.   assert(p3.OK());
  641.  
  642.   i = 0;
  643.   for (int it5 = p.low(); it5 < p.fence(); p.next(it5)) 
  644.   {
  645.     assert(p1.low() == it5);
  646.     p1.del_low();
  647.     assert(!p1.valid(it5));
  648.     p2.del_high ();
  649.     p3 [it5] = -it5;
  650.     ++i;
  651.   }
  652.   assert(i == 50);
  653.  
  654.   assert(p.OK());
  655.   assert(p1.OK());
  656.   assert(p2.OK());
  657.   assert(p3.OK());
  658.   assert (p1.empty());
  659.   assert (p2.empty());
  660.  
  661.   p3.append (p);
  662.   assert(p3.OK());
  663.  
  664.   p1.prepend (p);
  665.   p2.append (p);
  666.   assert(p1.length() == p.length());
  667.   assert(p1.length() == p2.length());
  668.   assert(p1.OK());
  669.   assert(p2.OK());
  670.  
  671.   p2.clear();
  672.   assert(p2.OK());
  673.   assert(p2.empty());
  674.   p2 = p1;
  675.   assert(p2.OK());
  676.  
  677.  
  678.   p1 = p;
  679.   assert(p1.OK());
  680.   p1.reset_low(p1.low_element());
  681.   p1 [13] = 1313;
  682.   p1 [-7] = -7777;
  683.   p1 [-25] = -252525;
  684.   p1 [24] = 24242424;
  685.   assert(!p1.valid(25));
  686.   assert(!p1.valid(-26));
  687.   assert(p1.OK());
  688.  
  689.   p1 = p;
  690.   p1.reset_low (p1.low_element());
  691.   for (int it6 = p1.low(); it6 < p1.fence(); it6++)
  692.   {
  693.     assert (p1[it6] == it6);
  694.   }
  695.   p1.reverse();
  696.   i = p1.high();
  697.   for (it6 = p1.low(); it6 < p1.fence(); it6++)
  698.   {
  699.     assert (p1[it6] == i);
  700.     --i;
  701.   }
  702.   assert(p1.OK());
  703.  
  704. }
  705.  
  706.  
  707. main()
  708. {
  709.   cout << "FPtest\n"; FPtest();
  710.   cout << "XPtest\n"; XPtest();
  711.   cout << "MPtest\n"; MPtest();
  712.   cout << "RPtest\n"; RPtest();
  713.   cout << "\nend of tests\n";
  714. }
  715.